/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Factory.DAO.MySQL;

import DatabaseConnection.DBConnectionFactory;
import Factory.DAO.UserDefDAO;
import Factory.DAOFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Patrick
 */
public class MySQLUserDefDAO extends UserDefDAO{

    @Override
    public int getUserDefID(String userTypeName) {
        try {
            // TODO code application logic here
            DBConnectionFactory myFactory = DBConnectionFactory.getInstance(DAOFactory.MYSQL);
            Connection conn = myFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("select iduserdef from userdef where userdefname = ?");
            pstmt.setString(1, userTypeName);
            ResultSet rs = pstmt.executeQuery();
            int userTypeID = -1;
            while(rs.next()){
                userTypeID = rs.getInt("iduserdef");
            }
            conn.close();
            return userTypeID;
        } catch (SQLException ex) {
            Logger.getLogger(MySQLUserDefDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return -1;
    }

    @Override
    public String getUserDefName(int userDefID) {
        try {
            // TODO code application logic here
            DBConnectionFactory myFactory = DBConnectionFactory.getInstance(DAOFactory.MYSQL);
            Connection conn = myFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("select userdefname from userdef where iduserdef = ?");
            pstmt.setInt(1, userDefID);
            ResultSet rs = pstmt.executeQuery();
            String userTypeName = null;
            while(rs.next()){
                userTypeName = rs.getString("userdefname");
            }
            conn.close();
            return userTypeName;
        } catch (SQLException ex) {
            Logger.getLogger(MySQLUserDefDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
}
